﻿using Prism.Events;
using Prism.Mvvm;
using RoboAssembleX.Application.AddBoard1;
using RoboAssembleX.Application.AddBoard2;
using RoboAssembleX.Application.AddGlass;
using RoboAssembleX.Application.AddGlass1;
using RoboAssembleX.Application.AddNeedle1;
using RoboAssembleX.Application.AddNeedle2;
using RoboAssembleX.Application.Common;
using RoboAssembleX.Application.Robot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace RoboAssembleX.Application.ActionJobs
{
    public class ActionJob08 : BindableBase,IActionJob
    {
        private readonly IEventAggregator eventAggregator;
        private readonly RoboModel roboModel;
        private readonly AddBoard2Model addBoard2Model;
        private readonly AddNeedle1Service addNeedle1Service;
        private readonly RoboService roboService;
        private readonly AddNeedle1Model addNeedle1Model;

        public ActionJob08(IEventAggregator eventAggregator,RoboModel roboModel, RoboService roboService,
            AddNeedle1Model addNeedle1Model, AddBoard2Model addBoard2Model, AddNeedle1Service addNeedle1Service)
        {
            this.eventAggregator = eventAggregator;
            this.roboModel = roboModel;
            this.addBoard2Model = addBoard2Model;
            this.addNeedle1Service = addNeedle1Service;
            this.roboService = roboService;
            this.addNeedle1Model = addNeedle1Model;
        }
        private string name = "暂放到引线1"; public string Name { get => name; }
        private bool running; public bool Running { get => running; set { running = value; RaisePropertyChanged(); } }
        private bool enable = true; public bool Enable { get => enable;  set { enable = value; RaisePropertyChanged(); } }
        private int priority = 0; public int Priority { get => priority; set { priority = value; RaisePropertyChanged(); } }



        public bool IsReady
        {
            get
            {
                //暂放位置有料 引线1允许放料  引线1工位没有料
                return addBoard2Model.AllowTempPut
                     && addNeedle1Model.AllowPut
                     && !addNeedle1Model.PosUsed;
            }
        }

        public async Task RunActionAsync()
        {
            Running = true;
            eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("启动机械臂动作8!", 1));
            await  roboService.Action8Async();
            while(roboModel.DO8)
            {
               await  Task.Delay(100);
            }
            //玻璃珠1位有料 玻璃珠2位有料 玻璃珠允许放料  开始启动玻璃珠
            if (addNeedle1Model.AllowPut && addNeedle1Model.PosUsed)
            {
                eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("启动引线设备一设备!", 1));
                addNeedle1Service.Action();
            }
            await Task.Delay(100);
            Running = false;
        }
    }
}
